package com.jiazhong.Servlet;

import com.jiazhong.Model.Goods;
import com.jiazhong.Service.GoodsService;
import com.jiazhong.Service.Impl.GoodsServiceImpl;
import com.jiazhong.utils.Utils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.text.ParseException;
import java.util.List;

@WebServlet("/goods/goods.do")
public class GoodsServlet extends BaseServlet{
    //创建GoodsService接口的实现类对象
    GoodsService goodsService=new GoodsServiceImpl();
    /**
     * 查询所有商品的Servlet方法
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    protected void queryGoods(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        List<Goods> goodsList=goodsService.queryGoods();
        //将goodsList对象存入到request中
        request.setAttribute("goodsList",goodsList);
        request.getRequestDispatcher("/Goods/showGoods.jsp").forward(request,response);
    }


    protected void queryGoodsById(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取goods_id
        int goods_id=Integer.parseInt(request.getParameter("goods_id"));
//        System.out.println(goods_id);
        Goods goods=goodsService.queryGoodsById(goods_id);
        request.setAttribute("goods",goods);
//        System.out.println(goods.getGoods_id()+""+goods.getGoods_name());
        request.getRequestDispatcher("/Goods/updateGoods.jsp").forward(request,response);
    }


    protected void delGoodsById(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获得客户端发送的请求数据
        int goods_id = Integer.parseInt(request.getParameter("goods_id"));
        System.out.println(goods_id);
        //调用GoodsService中关于删除商品的方法
        goodsService.delGoodsById(goods_id);
        //根据结果跳转到展示页面
        response.sendRedirect("/goods/queryGoods.do");
    }

    protected void updateGoods(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try {
            request.setCharacterEncoding("utf-8");
            Goods goods= Utils.encapsulationRequestToBean(Goods.class,request);

            goodsService.updateGoods(goods);
            response.sendRedirect("/goods/queryGoods.do");
        } catch (ParseException e) {
            e.printStackTrace();
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    }
}
